package com.kaka.bus.engine.borrow.face;

import android.app.Activity;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zhangqi on 2016/10/11.
 */

public class FacePresenter extends RxPresenter<FaceContract.View> implements FaceContract.Presenter{

    @Inject
    public FacePresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void commitPhotoData(byte[] photoData, byte[] photoEnv, String delta, String userId, String token) {
        Map<String, RequestBody> map = new HashMap<>();
        map.put("image_best"+"\"; filename=\""+"image_best.png", RequestBody.create(MediaType.parse("image/png"),photoData));
        map.put("image_env"+"\"; filename=\""+"image_env.png", RequestBody.create(MediaType.parse("image/png"),photoEnv));
        mView.showDialog("验证人脸识别");
        addSubscribe(mDataManager
                .getApi()
                .commitLiveness(userId,token,delta,map)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> data) {
                        if(Constant.SUCCESS_CODE.equals(data.getCode())){
                            mView.commitPhotoSuccess(data);
                        }else{
                            mView.Faild(data);
                        }
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void router(Activity activity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(activity, mDataManager.getApi(),mCompositeDisposable, () -> {
            if(mView != null){
                mView.dissDialog();
            }
        });
    }
}
